﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;

namespace TangoFamilyAndGuide.Site
{
    public partial class HousingSeleccionar : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                try
                {
                    string a = HttpContext.Current.Request.Url.Query.ToString().Replace("?", String.Empty);
                    switch (a)
                    {
                        case "Modify":
                            TituloPaginaLabel.Text = "Modify Milonguero Housing Network";
                            ViewState["IrA"] = "~/HousingModificar.aspx";
                            break;
                        case "ChangeStatus":
                            TituloPaginaLabel.Text = "Change Status Milonguero Housing Network";
                            ViewState["IrA"] = "~/HousingBaja.aspx";
                            break;
                        case "Images":
                            TituloPaginaLabel.Text = "Images Milonguero Housing Network";
                            ViewState["IrA"] = "~/HousingImagenes.aspx";
                            break;
                    }
                    DataBind();
                    if (Funciones.SessionTimeOut(Session))
                    {
                        Response.Redirect("~/SessionTimeout.aspx");
                    }
                    else
                    {
                        DireccionRadioButton.Checked = true;
                        TipoBusquedaRadioButton_CheckedChanged(DireccionRadioButton, new EventArgs());
                        DireccionTextBox.Focus();
                    }
                }
                catch (Exception ex)
                {
                    MensajeLabel.Text = EX.Funciones.Detalle(ex);
                }
            }
        }
        protected void BuscarButton_Click(object sender, EventArgs e)
        {
            if (Funciones.SessionTimeOut(Session))
            {
                Response.Redirect("~/SessionTimeout.aspx");
            }
            else
            {
                Entidades.Sesion sesion = (Entidades.Sesion)Session["Sesion"];
                List<Entidades.Housing> lista = new List<Entidades.Housing>();
                MensajeLabel.Text = String.Empty;
                if (IdHousingRadioButton.Checked)
                {
                    if (IdHousingTextBox.Text.Equals(String.Empty))
                    {
                        MensajeLabel.Text = "Uninformed " + IdHousingRadioButton.Text;
                        return;
                    }
                    else
                    {
                        try
                        {
                            lista = RN.Housing.ListaPorIdHousing(Convert.ToInt32(IdHousingTextBox.Text), true, sesion);
                        }
                        catch
                        {
                        }
                    }
                }
                else if (DireccionRadioButton.Checked)
                {
                    if (DireccionTextBox.Text.Equals(String.Empty))
                    {
                        MensajeLabel.Text = "Uninformed " + DireccionRadioButton.Text;
                        return;
                    }
                    else
                    {
                        lista = RN.Housing.ListaPorDireccion(DireccionTextBox.Text, true, sesion);
                    }
                }
                else
                {
                    if (PaisTextBox.Text.Equals(String.Empty))
                    {
                        MensajeLabel.Text = "Uninformed " + PaisRadioButton.Text;
                        return;
                    }
                    else
                    {
                        lista = RN.Housing.ListaPorPais(PaisTextBox.Text, true, sesion);
                    }
                }
                if (lista.Count == 0)
                {
                    HousingsGridView.DataSource = null;
                    HousingsGridView.DataBind();
                    MensajeLabel.Text = "No records found that meet the search.";
                }
                else if (lista.Count == 1)
                {
                    Session["Housing"] = lista[0];
                    Response.Redirect(ViewState["IrA"].ToString());
                }
                else
                {
                    HousingsGridView.DataSource = lista;
                    ViewState["Housings"] = lista;
                    HousingsGridView.DataBind();
                }
            }
        }
        protected void TipoBusquedaRadioButton_CheckedChanged(object sender, EventArgs e)
        {
            HousingsGridView.DataSource = null;
            HousingsGridView.DataBind();
            MensajeLabel.Text = String.Empty;
            if (IdHousingRadioButton.Checked)
            {
                DireccionTextBox.Text = String.Empty;
                PaisTextBox.Text = String.Empty;
                DireccionTextBox.Visible = false;
                PaisTextBox.Visible = false;

                IdHousingTextBox.Visible = true;
            }
            else if (DireccionRadioButton.Checked)
            {
                IdHousingTextBox.Text = String.Empty;
                PaisTextBox.Text = String.Empty;
                IdHousingTextBox.Visible = false;
                PaisTextBox.Visible = false;

                DireccionTextBox.Visible = true;
            }
            else
            {
                IdHousingTextBox.Text = String.Empty;
                DireccionTextBox.Text = String.Empty;
                IdHousingTextBox.Visible = false;
                DireccionTextBox.Visible = false;

                PaisTextBox.Visible = true;
            }
        }
        protected void HousingsGridView_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            int item = Convert.ToInt32(e.CommandArgument);
            List<Entidades.Housing> lista = (List<Entidades.Housing>)ViewState["Housings"];
            Entidades.Housing Housing = lista[item];
            switch (e.CommandName)
            {
                case "Seleccionar":
                    Session["Housing"] = Housing;
                    Response.Redirect(ViewState["IrA"].ToString());
                    break;
            }
        }
        protected void HousingsGridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                if (e.Row.Cells[6].Text != "Active")
                {
                    e.Row.ForeColor = Color.Red;
                }
            }
        }
    }
}